import Vue from 'vue'
import Router from 'vue-router'
import Home from "../views/home.vue";
import Podcasts from "../views/podcasts.vue";
import User from "../views/user.vue";
import Concern from "../views/concern.vue";
import Community from "../views/community.vue";
import Search from "../views/search/index.vue";
import Login from "../views/login/login.vue";
import {getCookie} from "../utils/cookie";
import * as cookie from "../utils/cookie";
import store from "../store";
import delPlayList from "../views/playlist/delPlayList.vue";
import updatePlayList from "../views/playlist/updatePlayList.vue";
import recommendSongs from "../views/recommend/recommendSongs.vue";
import playMusic from "../components/PlayMusic.vue";
import musicPlayBack from "../views/musicPlayBack.vue";
import comment from "../views/comment/index.vue";
import catList from "../views/playlist/catList.vue";
import playlistDetail from "../views/playlist/playlistDetail.vue";
import catTabs from "../views/playlist/catTabs.vue";
import topPlayList from "../views/playlist/topPlayList.vue";
import artistList from "../views/artist/artistList.vue";
import artistIndex from "../views/artist/index.vue";

Vue.use(Router)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    name: 'home',
    path: '/home',
    component: Home,
  }, {
    name: 'playMusic',
    path: '/playMusic',
    component: playMusic,
  },
  {
    name: 'catList',
    path: '/catList',
    component: catList,
    meta: {
      tabbar: false,
    },
  }, {
    name: 'artistIndex',
    path: '/artistIndex',
    component: artistIndex,
    meta: {
      tabbar: false,
    },
  }, {
    name: 'artistList',
    path: '/artistList',
    component: artistList,
    meta: {
      tabbar: false,
    },
  }, {
    name: 'topPlayList',
    path: '/topPlayList',
    component: topPlayList,
    meta: {
      tabbar: false,
    },
  }, {
    name: 'catTabs',
    path: '/catTabs',
    component: catTabs,
    meta: {
      tabbar: false,
    },
  }, {
    name: 'playlistDetail',
    path: '/playlistDetail',
    component: playlistDetail,
    meta: {
      tabbar: false,
    },
  },
  {
    name: 'comment',
    path: '/comment',
    component: comment,
    meta: {
      tabbar: false,
      requireAuth: true
    },
  },
  {
    name: 'musicPlayBack',
    path: '/musicPlayBack',
    component: musicPlayBack,
    meta: {
      tabbar: false,
      requireAuth: true
    },
  },
  {
    name: 'podcasts',
    path: '/podcasts',
    component: Podcasts,
  },
  {
    name: 'delPlayList',
    path: '/delPlayList',
    component: delPlayList,
    meta: {
      tabbar: false,
      requireAuth: true
    },
  }, {
    name: 'recommendSongs',
    path: '/recommendSongs',
    component: recommendSongs,
    meta: {
      tabbar: false,
      requireAuth: true
    },
  },
  {
    name: 'updatePlayList',
    path: '/updatePlayList',
    component: updatePlayList,
    meta: {
      tabbar: false,
      requireAuth: true
    },
  },
  {
    name: 'user',
    path: '/user',
    component: User,
    meta: {
      requireAuth: true
    },

  }, {
    name: 'login',
    path: '/login',
    component: Login,
    meta: {
      tabbar: false
    }
  },
  {
    name: 'concern',
    path: '/concern',
    component: Concern,
  },
  {
    name: 'community',
    path: '/community',
    component: Community,
  },
  {
    name: 'search',
    path: '/search',
    component: Search,
    meta: {
      tabbar: false
    }
  }
]

const router = new Router({
  // 设定history的模式
  mode: 'history',
  routes
})
//在每次访问路由前调用
router.beforeEach((to, from, next) => {
  //如果当前要跳转的路由需要认证，则认证
  if (to.meta.requireAuth == true) {
    //获取当前浏览器cookie是否存在账号
    let curAccount = window.localStorage.getItem("account")
    //如果cookie中保存有登录信息，就说明登陆过，放行
    if (curAccount) {
      next()
      //如果没有登录到，就强制重定向到login
    } else {
      next({
        name: 'login',
        //下面的query参数会让地址栏多一个重定向提示
        //可以注释掉，这样地址栏会简洁一点
        query: {
          redirect: to.fullPath
        }
      })
    }
    //如果不需要认证就放行
  } else {
    next()
  }
})
export default router
